package com.tanhua.server.controller;

import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 获取用户信息
 */
@RestController
@RequestMapping("/messages")
@Slf4j
public class MessageController {


    @Autowired
    private MessagesService messagesService;

    /**
     * 通过环信id查询用户信息
     * @param huanxinId
     * @return
     */
    @GetMapping("/userinfo")
    public ResponseEntity getUserInfo(String huanxinId){
        UserInfoVo vo = messagesService.findUserByHuanxinUser(huanxinId);
        return ResponseEntity.ok(vo);
    }


    /**
     * 添加好友
     * @return
     */
    @RequestMapping(value = "/contacts",method = RequestMethod.POST)
    public ResponseEntity addContacts(@RequestBody Map<String,String> params){
        Long friendUserId = Long.valueOf(params.get("userId"));//好友用户id
        messagesService.addContacts(friendUserId);
        return ResponseEntity.ok(null);
    }


    /**
     * 联系人列表分页查询
     * @return
     */
    @RequestMapping(value = "/contacts",method = RequestMethod.GET)
    public ResponseEntity findPageContacts(@RequestParam(value = "page",defaultValue = "1") Long page, @RequestParam(value = "pagesize",defaultValue = "10")Long pageSize,String keyword){
        PageResult<ContactVo> pageResult = messagesService.findPageContacts(page,pageSize,keyword);
        return ResponseEntity.ok(pageResult);
    }
}
